﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using NLog;
using NLog.Config;
using NLog.Targets;

namespace  HDJ.Framework.Core
{
    /// <summary>
    /// A core logger that can be used to write log messages to the console and or a log file.
    /// </summary>
    public class NLoggerHandler : ILogHandler
    {
        private NLog.Logger logger;
        public NLoggerHandler()
        {
            LoggingConfiguration logConfig = new LoggingConfiguration();
            FileTarget fileTarget = new FileTarget();
            string path = "";
            RuntimePlatform platform = Application.platform;
            if (platform == RuntimePlatform.WindowsEditor ||platform == RuntimePlatform.WindowsPlayer 
                ||platform == RuntimePlatform.OSXEditor ||platform == RuntimePlatform.OSXPlayer
                ||platform == RuntimePlatform.LinuxEditor||platform== RuntimePlatform.LinuxPlayer
                )
            {
                path = Environment.CurrentDirectory+"/Logs/${shortdate}/Log - " + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".txt";
               
            }
            else
            {
                path = Application.persistentDataPath + "/Logs/${shortdate}/Log - " + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".txt";
            }
            Debug.Log("NlogPath:" + path);
            fileTarget.FileName = path;
            fileTarget.Layout = "${date:format=\\[HH\\:mm\\:ss\\]} [${threadid} - ${level}] - ${message}";
            ConsoleTarget consoleTarget = new ConsoleTarget();
            consoleTarget.Layout = "${date:format=HH\\:mm\\:ss} - ${message}";

            logConfig.AddTarget("file", fileTarget);
            logConfig.AddTarget("console", consoleTarget);

            logConfig.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, fileTarget));
            logConfig.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, consoleTarget));

            NLog.LogManager.Configuration = logConfig;
            logger = NLog.LogManager.GetCurrentClassLogger();
            NLog.LogManager.EnableLogging();
            //Debug.Log("NLog handle");
        }

        public void LogFormat(LogType logType, UnityEngine.Object context, string format, params object[] args)
        {

          
            switch (logType)
            {
                case LogType.Assert:
                    logger.Info(format, args);
                    break;
                case LogType.Log:
                    logger.Debug(format,args);
                    break;
                case LogType.Warning:
                    logger.Warn(format, args);
                    break;
                case LogType.Error:
                    logger.Error(format, args);
                    break;
                case LogType.Exception:
                    logger.Fatal(format, args);
                    break;
            }
          
        }

        public void LogException(Exception exception, UnityEngine.Object context)
        {
            logger.Trace(exception);
        }

        public void Close()
        {
            NLog.LogManager.Shutdown();
        }
    }
}
